import java.util.*;

public class PersonalRecommender implements Recommender {
    private Map<String, LinkedList<String>> likes = new HashMap<>();

    @Override
    public void addLikes(String name, String item) {
        LinkedList<String> linkedList = likes.get(name);
        if (linkedList == null){
            linkedList = new LinkedList<>();
        }
        for (String s : linkedList) {
            if (likes.get(name).contains(item)){
                return;
            }
        }
        linkedList.add(item);
        likes.put(name,linkedList);
    }

    @Override
    public boolean likeBoth(String name, String item1, String item2) {
        if (!likes.containsKey(name)) {
            throw new UnknownPersonException("未找到此人");
        }
        return likes.get(name).contains(item1) && likes.get(name).contains(item2);
    }

    @Override
    public LinkedList<String> recommendByPerson(String name) {
        Set<String> hashSet = new HashSet<>();

        for (String s : likes.get(name)) {
            hashSet.addAll(recommendByProject(s));
        }
        return new LinkedList<>(hashSet);
    }

    @Override
    public LinkedList<String> recommendByProject(String item) {
        Set<String> hashSet = new HashSet<>();
        Set<String> names = likes.keySet();
        for (String name : names) {
            if (likes.get(name).contains(item)){
                hashSet.addAll(likes.get(name));
            }
        }
        hashSet.remove(item);
        return new LinkedList<>(hashSet);
    }

    public Map<String, LinkedList<String>> getLikes() {
        return likes;
    }

    public void setLikes(Map<String, LinkedList<String>> likes) {
        this.likes = likes;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        PersonalRecommender that = (PersonalRecommender) o;
        return Objects.equals(likes, that.likes);
    }

    @Override
    public int hashCode() {
        return Objects.hash(likes);
    }

    @Override
    public String toString() {
        return "PersonalRecommender{" +
                "likes=" + likes +
                '}';
    }

    public static void main(String[] args) {
        PersonalRecommender personalRecommender = new PersonalRecommender();
        LinkedList<String> linkedList1 = new LinkedList<>();
        LinkedList<String> linkedList2 = new LinkedList<>();
        LinkedList<String> linkedList3 = new LinkedList<>();

        linkedList1.add("1");
        linkedList1.add("2");
        linkedList1.add("3");
        linkedList1.add("4");
        linkedList1.add("5");

        linkedList2.add("a");
        linkedList2.add("b");
        linkedList2.add("c");
        linkedList2.add("3");

        linkedList3.add("a");

        Map<String,LinkedList<String>> map = new HashMap<>();
        map.put("A",linkedList1);
        map.put("B",linkedList2);
        map.put("C",linkedList3);

        personalRecommender.setLikes(map);

        personalRecommender.addLikes("A","e");
        System.out.println(personalRecommender.likes);

        System.out.println(personalRecommender.likeBoth("A","1","e"));
        System.out.println(personalRecommender.likeBoth("C","1","a"));

        System.out.println(personalRecommender.recommendByPerson("B"));

        System.out.println(personalRecommender.recommendByProject("3"));
    }
}
